package ace.cmp.i18n.core.impl;

import ace.cmp.core.exception.BusinessException;
import ace.cmp.i18n.api.AceMessageSource;
import ace.cmp.i18n.api.MessageSourceAssertService;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author Caspar
 * @create 2021/2/24 断言检查，抛出{@link BusinessException} 异常
 */
public class DefaultMessageSourceAssertService implements MessageSourceAssertService {
  private final AceMessageSource messageSource;

  public DefaultMessageSourceAssertService(AceMessageSource messageSource) {
    this.messageSource = messageSource;
  }

  @Override
  public void isTrue(boolean value, String code, Object[] args, String defaultMessage) {
    this.isTrue(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isTrue(boolean value, String code, String defaultMessage) {
    this.isTrue(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isTrue(boolean value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isTrue(
      boolean value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    if (value) {
      String message = this.messageSource.getMessage(code, args, defaultMessage);
      consumer.accept(message);
    }
  }

  @Override
  public void isFalse(boolean value, String code, Object[] args, String defaultMessage) {
    this.isFalse(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isFalse(boolean value, String code, String defaultMessage) {
    this.isFalse(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isFalse(
      boolean value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isFalse(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isFalse(
      boolean value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(!value, code, args, defaultMessage, consumer);
  }

  @Override
  public void isNull(Object value, String code, Object[] args, String defaultMessage) {
    this.isNull(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isNull(Object value, String code, String defaultMessage) {
    this.isNull(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isNull(Object value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isNull(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isNull(
      Object value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(Objects.isNull(value), code, args, defaultMessage, consumer);
  }

  @Override
  public void isNotNull(Object value, String code, Object[] args, String defaultMessage) {
    this.isNotNull(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isNotNull(Object value, String code, String defaultMessage) {
    this.isNotNull(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isNotNull(
      Object value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isNotNull(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isNotNull(
      Object value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(Objects.nonNull(value), code, args, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(String value, String code, Object[] args, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(String value, String code, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(String value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isEmpty(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(
      String value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(value == null || value.length() == 0, code, args, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(Collection value, String code, Object[] args, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(Collection value, String code, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(
      Collection value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isEmpty(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(
      Collection value,
      String code,
      Object[] args,
      String defaultMessage,
      Consumer<String> consumer) {
    this.isTrue(value == null || value.size() == 0, code, args, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(Map value, String code, Object[] args, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        args,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(Map value, String code, String defaultMessage) {
    this.isEmpty(
        value,
        code,
        null,
        defaultMessage,
        (message) -> {
          throw new BusinessException(message);
        });
  }

  @Override
  public void isEmpty(Map value, String code, String defaultMessage, Consumer<String> consumer) {
    this.isEmpty(value, code, null, defaultMessage, consumer);
  }

  @Override
  public void isEmpty(
      Map value, String code, Object[] args, String defaultMessage, Consumer<String> consumer) {
    this.isTrue(value == null || value.size() == 0, code, args, defaultMessage, consumer);
  }
}
